<template>
	<view class="body">
		<myHead :title="title" />
		  <uni-forms :value="formData" ref="form">
			<uni-forms-item class="addTask" label="任务名称" name="taskList">
				<uni-easyinput :border="false" type="text" v-model="taskName" placeholder="请输入任务名"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item class="addTask" label="执行人" name="taskList">
				<picker :value="executorsIndex"
					:range="executors" range-key="userName" @change="select">
					<view class="uni-input">{{executors[executorsIndex].userName}}</view>
				</picker>
			</uni-forms-item>
			<uni-forms-item class="addTask" label="监督人" name="taskList">
					<picker  :value="monitorsIndex"
						:range="monitors" range-key="userName"
						@change="selectmonitorsIndex">
						<view class="uni-input">{{monitors[monitorsIndex].userName}}</view>
					</picker>
			</uni-forms-item>
			
			<uni-forms-item class="addTask" label="任务分数" name="taskList">
				<uni-easyinput :border="false" type="number" v-model="number" ></uni-easyinput>
			</uni-forms-item>
		</uni-forms>
		
		<button @click="submitForm">新增</button>
	</view>
</template>

<script>
	import myHeader from '../components/head.vue'
	export default {
		data(){
			return {
				title:"新增任务",
				taskName:'',//任务名称
				number:0,//分数
				executorsIndex:0,
				monitorsIndex:0,
				executors: [],
				monitors:[],
				formData:{},
			}
		},components:{
				myHead:myHeader,
		},
		onLoad(e) {
			this.executors = JSON.parse(e.executors)
			this.monitors = JSON.parse(e.monitors)
			
			console.log(this.executors,this.monitors)
		},
		onReady() {
			
		},
		methods: {
			select(e){
				 this.executorsIndex = e.target.value
			},
			selectmonitorsIndex(e){
				 this.monitorsIndex = e.target.value
			},
			submitForm(){
				if(Number.parseInt(this.number)>=0 && this.taskName.length>0){
					let pages = getCurrentPages();  //获取所有页面栈实例列表
					let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
					let task = {
							taskName:this.taskName,
							executorUser:this.executors[this.executorsIndex],
							monitorUser:this.monitors[this.monitorsIndex],
							number:Number.parseInt(this.number)
					}
					console.log(prevPage)
					prevPage.$vm.taskList.push(task);   //修改上一页data里面的
					uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: 1
					});
				}else{
					uni.showToast({
						title:'请正确输入任务名称和分数后提交',
						duration:2000,
						icon:'none'
					})
				}
			}
		},
	
	}
</script>
<style lang="scss" scoped>	

	/**
	 * 定义按钮样式
	 */
	@mixin button-commit {
	  background: linear-gradient(180deg, #ff5e59, #e20909);
	  color: #fff;
	}
	.body{
		height: 100%;
		background-color:#f1f1f1 ;
		button{
			@include button-commit;
			width: 90%;
		}
		/deep/ .uni-forms {
			.uni-forms-item {
				background-color: #FFFFFF;
				margin: 5% 0;
				padding: 3% 5%;
			}
			.uni-forms-item__label{
				width:auto;
			}
			.uni-forms-item__inner{
				padding-bottom: 0px;
			}
			.uni-forms-item__content{
				line-height: 35px;
			}
			.uni-input{
				border: 1px solid #e5e5e5;
				border-color: rgb(229, 229, 229);
				padding: 0 5%;
			}
		}
	}
	
</style>